// pages/redenvelope/index.js
const app = getApp() // 引入app
import _request from '../../utils/request.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl, //全局img路径
    RedbagList: [],
    CouponList: [],
    show: false,
    invitecode: '',
    active: 0
  },

  // 点击关闭红包页
  onClickHide() {
    this.setData({
      show: false
    });
  },

  // 前往发红包页面
  toGive() {
    wx.navigateTo({
      url: '/pages/redenvelope/giveredenvelope/index',
    })
  },

  // 去首页使用
  toHome() {
    wx.switchTab({
      url: '/firstpages/home/index',
    })
  },

  // 详情页
  onDetails(e) {
    const {
      item
    } = e.currentTarget.dataset
    let obj = JSON.stringify(item)
    wx.navigateTo({
      url: '/pages/redenvelope/details/index?data=' + obj,
    })
  },

  // 删除红包
  onDlete(e) {
    _request.postObj('/redbagShare/delete', {
      redbagId: e.currentTarget.dataset.id,
      userId: wx.getStorageSync('userid'),
    }).then((res) => {}).catch((error) => {
      console.log(error, 'error');
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getRedbagShare(1, false);
    this.getUserCoupon(false);
    let vm = this
    // 领取红包
    if (options.redbagId) {
      _request.postObj('/redbagShare/draw', {
        redbagId: options.redbagId,
        userId: wx.getStorageSync('userid'),
      }).then((res) => {
        // 成功回调获取金额
        _request.postObj('/redbagShare/getDetail', {
          redbagId: options.redbagId,
          userId: wx.getStorageSync('userid'),
        }).then((res) => {
          let data = res.data.data.redbagDrawRecords.filter(item => {
            return item.userId == wx.getStorageSync('userid')
          })
          vm.setData({
            show: true,
            drawList: res.data.data,
            drawmoney: data
          })
        }).catch((error) => {
          console.log(error, 'error');
        })
      }).catch((error) => {
        console.log(error, 'error');
      })
    }
    // 获取邀请码
    _request.get('/recomUser/findReCode', {
      userId: wx.getStorageSync('userid'),
    }).then((res) => {
      this.setData({
        invitecode: res.data.data.invitecode
      })
    }).catch((error) => {
      console.log(error, 'error');
    })

  },

  // 获取发出红包列表
  getRedbagShare(e, show) {
    if (show) {
      wx.showNavigationBarLoading() // 顶部加载状态
    }
    _request.get('/redbagShare/list', {
      type: 0,
      pageNo: e,
      pageSize: 10,
      userId: wx.getStorageSync('userid'),
    }).then((res) => {
      this.setData({
        RedbagList: res.data.data
      })
      if (show) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    }).catch((error) => {
      if (show) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
      console.log(error, 'error');
    })
  },

  // 获取现金券 
  getUserCoupon(show) {
    if (show) {
      wx.showNavigationBarLoading() // 顶部加载状态
    }
    _request.get('/userCoupon/getCashCoupon', {
      userId: wx.getStorageSync('userid'),
    }).then((res) => {
      this.setData({
        CouponList: res.data.data
      })
      if (show) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    }).catch((error) => {
      if (show) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
      console.log(error, 'error');
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      active: Number(app.globalData.redenvelopeactive)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    let redbagId = res.target.dataset.id;
    let userid = wx.getStorageSync('userid');
    const {
      invitecode
    } = this.data
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    let a = '/pages/redenvelope/index?template=7&share=BAGRED&userId=' + userid + "&redbagId=" + redbagId + "&referralCode=" + invitecode
    return {
      title: "我在意约发了个大红包！", // 默认是小程序的名称
      path: '/pages/redenvelope/index?template=7&share=BAGRED&userId=' + userid + "&redbagId=" + redbagId + "&referralCode=" + invitecode,
      imageUrl: '/images/hblogo.png',
      success: function (res) {
      }
    }
  }
})